package com.wechat.page.contact.misc;

import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UICell;
import com.swak.fx.support.ui.UIIcon;
import com.swak.fx.support.ui.UILabel;
import com.swak.fx.support.ui.UILifecycle;
import com.swak.fx.support.ui.UITextArea;
import com.swak.fx.support.ui.UITextField;
import com.swak.utils.StringUtils;

import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

public class ProfileForm extends GridPane implements UILifecycle {

	private final String[] STYLE_CLASS = { "profile_form" };

	private UICell aliasCell;
	private UILabel aliasCell_label;
	private UILabel aliasCell_value;
	private UITextField aliasCell_field;
	private UIIcon aliasCell_ops;

	private UICell remarksCell;
	private UILabel remarksCell_label;
	private UILabel remarksCell_value;
	private UITextArea remarksCell_area;
	private UIIcon remarksCell_ops;

	public ProfileForm() {
		this.getStyleClass().addAll(STYLE_CLASS);
		this.aliasCell = new UICell();
		this.aliasCell_label = new UILabel("备注名：");
		this.aliasCell_value = new UILabel("点击添加备注");
		this.aliasCell_field = new UITextField().MaxWord(30);
		this.aliasCell_field.setPromptText("添加备注");
		this.aliasCell_ops = new UIIcon().Icon(UI.Icons.EDIT).fixedWidth();

		this.remarksCell = new UICell();
		this.remarksCell_label = new UILabel("描述：");
		this.remarksCell_value = new UILabel("点击添加描述");
		this.remarksCell_area = new UITextArea().MaxWord(120).WordLimit();
		this.remarksCell_area.setPromptText("添加描述");
		this.remarksCell_ops = new UIIcon().Icon(UI.Icons.EDIT).fixedWidth();

		this.init();
	}

	@Override
	public void initializeParts() {
		this.getChildren().add(this.aliasCell);
		GridPane.setConstraints(this.aliasCell, 0, 0);
		GridPane.setHgrow(this.aliasCell, Priority.ALWAYS);
		GridPane.setVgrow(this.aliasCell, Priority.ALWAYS);

		this.getChildren().add(this.remarksCell);
		GridPane.setConstraints(this.remarksCell, 0, 1);
		GridPane.setHgrow(this.remarksCell, Priority.ALWAYS);
		GridPane.setVgrow(this.remarksCell, Priority.ALWAYS);
	}

	@Override
	public void setupBindings() {
		this.aliasCell_value.textProperty().bind(Bindings.createStringBinding(() -> {
			String text = this.aliasProperty.get();
			return StringUtils.defaultIfBlank(text, "点击添加备注");
		}, this.aliasProperty));

		this.remarksCell_value.textProperty().bind(Bindings.createStringBinding(() -> {
			String text = this.remarksProperty.get();
			return StringUtils.defaultIfBlank(text, "点击添加描述");
		}, this.remarksProperty));

		this.aliasCell_field.registerEntryEventHandler(() -> {
			this.aliasEditingProperty.set(false);
		});

		this.remarksCell_area.registerEntryEventHandler(() -> {
			this.remarksEditingProperty.set(false);
		});
	}

	@Override
	public void setupEventHandlers() {
		this.aliasCell.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			if (!this.aliasEditingProperty.get()) {
				e.consume();
			}
			this.aliasEditingProperty.set(true);
		});
		this.remarksCell.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			if (!this.remarksEditingProperty.get()) {
				e.consume();
			}
			this.remarksEditingProperty.set(true);
		});
	}

	@Override
	public void setupValueChangedListeners() {
		UILifecycle.super.setupValueChangedListeners();
		this.aliasEditingProperty.addListener((ob, o, n) -> {
			if (n) {
				this.aliasCell.valueNodeProperty().set(this.aliasCell_field);
				this.aliasCell_field.setText(this.aliasProperty.get());
				this.aliasCell_field.requestFocus();
			} else {
				this.aliasCell.valueNodeProperty().set(this.aliasCell_value);
			}
		});
		this.aliasCell_field.focusedProperty().addListener((ob, o, n) -> {
			this.aliasEditingProperty.set(n);
			if (!n) {
				this.onAliasEdit(this.aliasCell_field.getText());
			}
		});
		this.remarksEditingProperty.addListener((ob, o, n) -> {
			if (n) {
				this.remarksCell.valueNodeProperty().set(this.remarksCell_area);
				this.remarksCell_area.setText(this.remarksProperty.get());
				this.remarksCell_area.requestFocus();
			} else {
				this.remarksCell.valueNodeProperty().set(this.remarksCell_value);
			}
		});
		this.remarksCell_area.focusedProperty().addListener((ob, o, n) -> {
			this.remarksEditingProperty.set(n);
			if (!n) {
				this.onRemarksEdit(this.remarksCell_area.getText());
			}
		});
	}

	public void onAliasEdit(String alias) {
		this.aliasEditProperty().set(StringUtils.trimToEmpty(alias));
	}

	public void onRemarksEdit(String remarks) {
		this.remarksEditProperty().set(StringUtils.trimToEmpty(remarks));
	}

	@Override
	public void initializeFinish() {
		this.aliasCell.textNodeProperty().set(this.aliasCell_label);
		this.aliasCell.valueNodeProperty().set(this.aliasCell_value);
		this.aliasCell.opsNodeProperty().set(this.aliasCell_ops);

		this.remarksCell.textNodeProperty().set(this.remarksCell_label);
		this.remarksCell.valueNodeProperty().set(this.remarksCell_value);
		this.remarksCell.opsNodeProperty().set(this.remarksCell_ops);
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private final BooleanProperty aliasEditingProperty = new SimpleBooleanProperty(false);
	private final BooleanProperty remarksEditingProperty = new SimpleBooleanProperty(false);
	private final StringProperty aliasProperty = new SimpleStringProperty();

	public StringProperty aliasProperty() {
		return aliasProperty;
	}

	private final StringProperty remarksProperty = new SimpleStringProperty();

	public StringProperty remarksProperty() {
		return remarksProperty;
	}

	// ================================================================================
	// 编辑
	// ================================================================================
	private final StringProperty aliasEditProperty = new SimpleStringProperty();

	public StringProperty aliasEditProperty() {
		return aliasEditProperty;
	}

	private final StringProperty remarksEditProperty = new SimpleStringProperty();

	public StringProperty remarksEditProperty() {
		return remarksEditProperty;
	}
}
